package com.baosight.risk.service.xtri.impl;

import com.alibaba.fastjson.JSON;
import com.baosight.risk.api.xtri.IXtriTempSystemService;
import com.baosight.risk.base.mapper.BaseMapper;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.utils.HttpClientUtil;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.mapper.xtaa.code.XtaaCodeMapper;
import com.baosight.risk.mapper.xtri.TXtriTempSystemMapper;
import com.baosight.risk.mapper.xtzy.XtzyCompanyMapper;
import com.baosight.risk.service.xtaa.code.entity.XtaaCode;
import com.baosight.risk.service.xtri.entity.TXtriTempSystem;
import com.baosight.risk.service.xtri.vo.XtriTempSystemVO;
import com.baosight.risk.service.xtzy.entity.XtzyCompany;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("xtriTempSystemServiceImpl")
public class XtriTempSystemServiceImpl extends BaseServiceImpl<TXtriTempSystem> implements IXtriTempSystemService {
    @Autowired
    private TXtriTempSystemMapper tempSystemMapper;

    @Autowired
    private XtaaCodeMapper xtaaCodeMapper;


    @Autowired
    private XtzyCompanyMapper companyMapper;

    @Override
    public BaseMapper getMapper() {
        return tempSystemMapper;
    }

    @Override
    @Transactional
    public void importSystem() throws Exception {
        XtaaCode codeParam = new XtaaCode();
        codeParam.setDdTypeCode("XTRI_TEMP_URL");
        codeParam.setDdCode("QUERY_SJ_1");
        List<XtaaCode> codeList = xtaaCodeMapper.select(codeParam);
        if (codeList.size() > 0) {
            String url = codeList.get(0).getDdDesc();
            String result = HttpClientUtil.doPostJson(url, "");
            JSONObject js = JSONObject.fromObject(result);

            if ("1".equals(js.getString("status"))) {
                tempSystemMapper.delete(new TXtriTempSystem());

                JSONArray jSONArray = js.getJSONArray("result");
                for (int i = 0; i < jSONArray.size(); i++) {
                    Map m = jSONArray.getJSONObject(i);
                    TXtriTempSystem tempSystem = BeanUtils.map2Object(m, TXtriTempSystem.class);
                    this.save(tempSystem);
                }
            } else {
                throw new Exception(js.getString("msg"));
            }
        }

    }


    @Override
    public TXtriTempSystem save(TXtriTempSystem tempSystem) {
        if("继续适用".equals(tempSystem.getFileStatus())){
            tempSystem.setFileStatusName(tempSystem.getFileStatus());
            tempSystem.setFileStatus("10");
        }else if("修订".equals(tempSystem.getFileStatus())){
            tempSystem.setFileStatusName(tempSystem.getFileStatus());
            tempSystem.setFileStatus("20");
        }else if("作废".equals(tempSystem.getFileStatus())){
            tempSystem.setFileStatusName(tempSystem.getFileStatus());
            tempSystem.setFileStatus("30");
        }else if("新增".equals(tempSystem.getFileStatus())){
            tempSystem.setFileStatusName(tempSystem.getFileStatus());
            tempSystem.setFileStatus("40");
        }
        tempSystem = (TXtriTempSystem)BeanUtils.initBean(tempSystem);
        tempSystemMapper.insertTempSystem(tempSystem);
        return tempSystem;
    }

    @Override
    public List<XtriTempSystemVO> queryTempSystem(XtriTempSystemVO tempSystem) {
        List<XtriTempSystemVO> systemsList = new ArrayList<XtriTempSystemVO>();
        servicePage();
        systemsList = tempSystemMapper.selectSystem(tempSystem);
        for (XtriTempSystemVO item : systemsList) {
            XtaaCode codeParam = new XtaaCode();
            codeParam.setDdTypeCode("XTRI_TEMP_FILEURl");
            codeParam.setDdCode("TYPE_FILE_URL");
            List<XtaaCode> codeList = xtaaCodeMapper.select(codeParam);
            if (codeList.size() > 0) {
                String url = codeList.get(0).getDdDesc();

                Map map=new HashMap();
                map.put("fileNumber",item.getFileNumber());
                map.put("fileVersion",item.getFileVersion());
                String  param= JSON.toJSONString(map);
//              String params = "{\"fileNumber\":\"" + item.getFileNumber() + "\",\"fileVersion\":\"" + item.getFileVersion() + "\"}";
                String result = HttpClientUtil.doPostJson(url, param);
                JSONObject js = JSONObject.fromObject(result);
                if ("1".equals(js.getString("status"))) {
                    JSONArray jSONArray = js.getJSONArray("result");
                    for (int i = 0; i < jSONArray.size(); i++) {
                        Map m = jSONArray.getJSONObject(i);
                        if (!ObjectUtils.isEmpty(m.get("previewUrl"))) {
                            item.setPreviewUrl(m.get("previewUrl").toString());
                        }
                    }

                }
            }

            XtzyCompany company = companyMapper.selectByPrimaryKey(item.getFileDraftDept());
            if (!ObjectUtils.isEmpty(company) &&"1".equals(company.getCompType())) {
                item.setCompNameL(company.getCompNameL());
            } else if (!ObjectUtils.isEmpty(company) && "4".equals(company.getCompType())) {
                company = companyMapper.getTopCompany(company.getPathCode(), Short.parseShort("1"));
                item.setCompNameL(company.getCompNameL());
            }

        }
            return systemsList;
        }

}
